lvalverdeb
Posts: 299
|
Posted: 06/11/2005, 1:22 AM |
|
Below is an implementation of phpguru's Treemenu (http://www.phpguru.org/static/treemenu.html) and Tree classes suitable for use with CCS. You must have the Treemenu classes in your project directory (folder named "treemenu" or modify the class to the correct path). You must also have the images files as well as treemenu.js
This wrapper class is designed to work with CCS's label control with its Content property set to HTML. Here are some basic intructions to get you up and running. The example uses the directory_categories table in the internetDB included with CCS samplepack.
1) Copy the class code below to an includable location e.g. Common.php
2) Create a label in a page/form and set its Content property to HTML. I've named the label menuLabel.
3) In the label's beforeshow event and the following code:
$tmp = new myTreeMenu(new clsDBinternetDB(),"directory_categories","category_id_parent","category_id","category_name","category_name");
$tmp->rootMenuLegend = "Directory Categories Menu"; // Root menu legend
$tmp->expandedbyDefault = false;
$tmp->setAllLinks = false; //false generates links at innermost level. Set to true to generate all links
$menuLabel->SetValue($tmp->generateTree());
unset($tmp);
4) Publish the page.
The class parameters are:
$connection - connection object
$table - table or view to be queried
$parentIdField - parent field name
$categoryField - primary key for menu items.
$descriptionField - field name to use for item descriptions
$urlField - field name containing url addresses to use
By default, the menu class assumes that the innermost item in the hierarchy is the url you want to go to. This behaviour can be overriden by setting the setAllLinks to true. Please also note that the class uses several CCS Studio functions e.g. CCDLookUp and a connection object based on CCS clsDBxxxx(). It is, of course, possible to use the class for purposes other than user menus but that will be up to you to figure out.
Please contact me at lvalverdeb@yahoo.com if you've any suggestions/comments.
Luis Valverde
San José, Costa Rica
treemenuclass.php
include_once("treemenu/TreeMenu.php");
include_once("treemenu/Tree.php"); //usually not required to be included here but just in case
class myTreeMenu {
var $connection, $table, $parentIdField, $categoryField, $descriptionField;
var $urlField,$keyValueType,$where,$order;
var $setAllLinks,$linkTarget,$imagesPath;
var $expandedByDefault,$defaultMenuClass;
function myTreeMenu($connection=null,$table="",$parentIdField="",$categoryField="",$descriptionField="",$urlField="") {
if ($connection !== null) {
$this->connection = $connection;
}
if (strlen($table))
$this->table = $table;
if (strlen($parentIdField))
$this->parentIdField = $parentIdField;
if (strlen($categoryField))
$this->categoryField = $categoryField;
if (strlen($descriptionField))
$this->descriptionField = $descriptionField;
if (strlen($urlField))
$this->urlField = $urlField;
$this->where = "";
$this->rootMenuLegend = "My Tree Menu";
$this->setMenuOptions();
}
function generateTree() {
//call this method from your label's BeforeShow event to generate the tree menu.
$this->_setMenuOptions();
return $this->_generateTree();
}
function setMenuOptions() {
// override these properties in your implementation class to customise the menu to your needs
$this->treeMenuFilesPath = "treemenu";
$this->order = $this->descriptionField;
$this->icon = "folder.gif";
$this->expandedIcon = "folder-expanded.gif";
$this->linkTarget = "_self";
$this->setAllLinks = false;
$this->expandedByDefault = false;
$this->defaultStyleClass = "treeMenuDefault";
$this->keyValueType = ccsInteger;
$this->HTMLStyle = "
<style type='text/css'>
body {
font-family: Comic Sans Serif;
font-size: 11pt;
font-color: #FFFFFF;
}
.treeMenuDefault {
font-style: Normal;
font-color: #FFFFFF;
}
.treeMenuBold {
font-color: #FFFFFF;
font-style: italic;
font-weight: bold;
}
</style>";
}
// I do not think there is a need to modify anything beyond this point.
function _generateTree() {
$category_id = 0;
$this->menu = new HTML_TreeMenu();
$this->menu->additem($this->_generateItems($category_id));
$treeMenu = &new HTML_TreeMenu_DHTML(
$this->menu,
array('images' => $this->imagesPath,
'linkTarget'=> $this->linkTarget,
'defaultClass' => $this->defaultStyleClass
)
);
$html.= $this->HTMLStyle;
$html.= $this->menuJS;
$html.=$treeMenu->printMenu();
$html.=' ';
return $html;
}
function &_generateItems($category_id) {
$db = $this->connection;
$sql = "SELECT "
.$this->categoryField.","
.$this->descriptionField.","
.$this->urlField.","
.$this->parentIdField
." FROM ".$this->table;
$where = (strlen($this->where)) ? $this->where:"";
if ($category_id == 0) {
$sqlwhere = "(".$this->parentIdField."= 0 OR ".$this->parentIdField." IS NULL)";
} else {
$sqlwhere = $this->parentIdField."=".$db->ToSQL($category_id,$this->keyValueType);
}
$where = (strlen($where)) ? $where." AND ".$sqlwhere:$sqlwhere;
$order = $this->order;
$sql = CCBuildSQL($sql,$where,$order);
$db->query($sql);
$node = &$this->_addNode($category_id);
while ($db->next_record()) {
$parent_id = $db->f($this->categoryField);
$newnode = &$this->_generateItems($parent_id);
$node->additem($newnode);
}
unset($db);
return $node;
}
function &_addNode($category_id) {
if ($category_id == 0 || $category_id == null) {
$nodeText = $this->rootMenuLegend;
$nodeLink = "";
} else {
$db = $this->connection;
$nodeText = CCDLookUp(
$this->descriptionField,
$this->table,
$this->categoryField."="
.$db->ToSQL($category_id,$this->keyValueType),
$db
);
$nodeUrl = CCDLookUp(
$this->urlField,
$this->table,
$this->categoryField."="
.$db->ToSQL($category_id,$this->keyValueType),
$db
);
if ($this->setAllLinks) {
$nodeLink = $nodeUrl;
} else {
//generate a link only if there are not subcategories
$nodeLink = (!$this->_hasSubCats($category_id)) ? $nodeUrl:"";
}
unset($db);
}
$node = &new HTML_TreeNode(
array(
'text' => $nodeText,
'link' => $nodeLink,
'icon' => $this->icon,
'expandedIcon' => $this->expandedIcon,
'expanded' => $this->expandedByDefault
)
);
return $node;
}
function _hasSubCats($category_id) {
$db = $this->connection;
$subcats = CCDLookup("count(*)",
$this->table,
$this->parentIdField."="
.$db->ToSQL($category_id,$this->keyValueType),
$db);
unset($db);
return $subcats;
}
function _setMenuOptions() {
//set/refresh runtime settings
$this->imagesPath=$this->treeMenuFilesPath."/images";
$this->menuJS = "<script src='"
.$this->treeMenuFilesPath
."/TreeMenu.js' language='JavaScript'
type='text/javascript'></script>";
}
}
_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4 |